package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.model.TbSpecificationOption;
import com.dongdong.model.TbSpecificationOptionExample;
import com.dongdong.service.SpecOptionService;
import com.dongdong.service.TypeTemplateService;
import com.dongdong.vo.SpecOptionVO;
import com.dongdong.vo.SpecVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecOptionServiceImpl implements SpecOptionService {

    @Autowired
    private TbSpecificationOptionMapper optionMapper;

    @Override
    public List<SpecOptionVO> getSpecOptionBySpecId(Long id) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = optionMapper.selectByExample(example);
        List<SpecOptionVO> collect = tbSpecificationOptions.stream().map(option -> {
            SpecOptionVO optionVO = new SpecOptionVO();
            BeanUtils.copyProperties(option, optionVO);
            return optionVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int deleteBySpecId(Long id) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        int i = optionMapper.deleteByExample(example);
        return i;
    }

    @Override
    public int insertSpecOption(SpecVO specVO) {
        List<SpecOptionVO> specOptionVOList = specVO.getSpecOptionVOList();
        int insert = 0;
        for (SpecOptionVO optionVO : specOptionVOList) {
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(optionVO, option);
            insert = optionMapper.insert(option);
        }
        if (insert > 0) {
            return insert;
        }
        return 0;
    }

    @Override
    public List<SpecOptionVO> getOptionList() {
        //获取所有规格选项
        List<TbSpecificationOption> optionlist = optionMapper.selectByExample(null);
        //转vo
        List<SpecOptionVO> collect = optionlist.stream().map(option -> {
            SpecOptionVO vo = new SpecOptionVO();
            BeanUtils.copyProperties(option, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }
}
